package email;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailSendService {
	private static EmailSendService instance = null;
	
	public static EmailSendService getInstance() {
		if (instance == null) {
			instance = new EmailSendService();
		}
		return instance;
	}
    
	private Properties properties;      
    private Session session;  
      
    private EmailSendService() {
      	init();
    }
    
    private void init() {
    	properties = new Properties();
    	try {
    	    properties.load(new FileInputStream("./konfig/email.properties"));
    	}
    	catch (IOException e) {
    		e.printStackTrace();
    	}

        session = Session.getDefaultInstance(properties);  
       // session.setDebug(true);
    }  
      
    public void sendEmail(String cabecera, String mezu, String to) {   
    	init();
        try{  
        	MimeMessage message = new MimeMessage(session);  
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
            message.setSubject(cabecera);  
            message.setText(mezu);  
            Transport t = session.getTransport("smtp");
            StringBuffer sb = new StringBuffer(properties.getProperty("password"));
            t.connect(properties.getProperty("user"), sb.reverse().toString());  
            t.sendMessage(message, message.getAllRecipients());
            t.close();  
        }
        catch (MessagingException me){
          	me.printStackTrace();       
        }       
    }  
    
      
}  